package com.zhang.zhangapp.view.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.Bill;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.adapter.BillsAdapter;
import com.zhang.zhangapp.view.custom.HeadRefreshView;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/23.
 */

@ContentView(R.layout.activity_mine_bills)
public class MineBillsActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.refresh_layout)
    PullToRefreshLayout refreshLayout;
    @ViewInject(R.id.bill_listview)
    ListView listView;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    BillsAdapter adapter;
    List<Bill> billList = new ArrayList<>();
    Map<String, String> params = new HashMap<>();

    MCallback callback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            Gson gson = new Gson();
            List<Bill> list = gson.fromJson(data, new TypeToken<List<Bill>>() {
            }.getType());
            if (list != null) {
                billList.addAll(list);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onSuccess(String message, String code) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {
            dismisionDalog();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initView();
        getComments(1);
    }

    @Event(type = View.OnClickListener.class, value = R.id.action_bar_left_image)
    private void back(View view) {
        this.finish();
    }

    private void initView() {
        titleText.setText("我的消费");
        adapter = new BillsAdapter(billList, this);
        listView.setAdapter(adapter);
        refreshLayout.setHeaderView(new HeadRefreshView(this));
        params.put("memberId", MApplication.getInstance().getUser().getMemberId());
        params.put("pageSize", BaseConfig.PAGE_SIZE + "");
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                getComments(1);
            }

            @Override
            public void loadMore() {
                getComments((billList.size() / BaseConfig.PAGE_SIZE + 1));
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MineBillsActivity.this,BillDetailActivity.class);
                intent.putExtra("billId",billList.get(i).getTransId());
                MineBillsActivity.this.startActivity(intent);
            }
        });
    }

    /**
     * get comment list
     *
     * @param pageNo
     */
    private void getComments(int pageNo) {
        if (pageNo == 1) {
            refreshLayout.setCanLoadMore(true);
            billList.clear();
            adapter.notifyDataSetChanged();
        }
        if (showLoadingDialog()) {
            params.put("pageNum", pageNo + "");
            CommonHttp.commonHttp("hzg/member/myBill", params, callback);
        }
    }
}
